unit UExecCmdDetailEdit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UExecCmDetailAddEditBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinOffice2016Colorful, Vcl.Menus, System.ImageList,
  Vcl.ImgList, cxImageList, Vcl.StdCtrls, cxButtons, Vcl.ExtCtrls, cxSpinEdit,
  cxMaskEdit, cxDropDownEdit, cxRadioGroup, cxGroupBox, cxCheckGroup,
  cxTextEdit, cxLabel, Data.DB, MemDS, DBAccess, Uni;

type
  TfrmExecCmdDetailEdit = class(TfrmExecCmdDetailAddEditBase)
    qryTmp: TUniQuery;
    procedure FormShow(Sender: TObject);
    procedure grpOutputPropertiesChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }

    procedure InitOutputport(sOutput: string);
  public
    { Public declarations }
  end;

var
  frmExecCmdDetailEdit: TfrmExecCmdDetailEdit;

implementation

uses
  UCommonConst, UCommonFunSqlite;

{$R *.dfm}

procedure TfrmExecCmdDetailEdit.btnSaveClick(Sender: TObject);
var
  sSql, sOutput: string;
  iIOID, iDelay, iSwitchState: Integer;
begin
  inherited;

    // devio
  if comDevIO.ItemIndex = -1 then
  begin
    MsgBoxDefault(RESDEVIONOSELECT);
    comDevIO.SetFocus;
    Abort;
  end;

  iIOID := Integer(comDevIO.Properties.Items.Objects[comDevIO.ItemIndex]);

  iDelay := edtDelay.Value;

  // input port
  sOutput := Trim(edtOutput.Text);

  if sOutput.IsEmpty then
  begin
    MsgBoxDefault(RESEXECCMDOUTPUTPORTEMPTY);
    grpOutput.SetFocus;
    Abort;
  end;

  // switchstate
  iSwitchState := grpSwitchState.ItemIndex;

  sSql := 'update ExecCmdDetail set Delay=' + QuotedStr(IntToStr(iDelay))
        + ',OutputPort=' + QuotedStr(sOutput)
        + ',SwitchState=' + QuotedStr(IntToStr(iSwitchState))
        + ',DevIOID=' + QuotedStr(IntToStr(iIOID))
        + ' where DetailID=' + QuotedStr(IntToStr(FSelectID));

  if ExecSql(sSql) then
  begin
    SendMessage(FParentHandle, WM_REFRESHDATA, 0, 0);

    MsgBoxDefault(RESSAVESUCCESS);
  end
  else
    MsgBoxDefault(RESSAVEFAIL);
end;

procedure TfrmExecCmdDetailEdit.FormCreate(Sender: TObject);
begin
  inherited;

  //
end;

procedure TfrmExecCmdDetailEdit.FormShow(Sender: TObject);
var
  sSql: string;
  iIOID: Integer;
begin
  inherited;

  sSql := 'select * from ExecCmdDetail where DetailID=''' + IntToStr(FSelectID) + '''';

  with qryTmp do
  begin
    close;
    SQL.Text := sSql;
    Open;

    if not Eof then
    begin
      edtDelay.Value := FieldByName('Delay').AsInteger;

      iIOID := FieldByName('DevIOID').AsInteger;
      LocationCom(comDevIO, iIOID);

      edtOutput.Text := Trim(FieldByName('OutputPort').AsString);
      InitOutputport(edtOutput.Text);
    end;
  end;
end;

procedure TfrmExecCmdDetailEdit.grpOutputPropertiesChange(Sender: TObject);
begin
  inherited;

  //
end;

procedure TfrmExecCmdDetailEdit.InitOutputport(sOutput: string);
var
  i, iPos: Integer;
  lst: TStringList;
begin
  lst := TStringList.Create;

  try
    lst.Delimiter := ';';
    lst.DelimitedText := sOutput;

    for i := 0 to lst.Count - 1 do
    begin
      iPos := StrToInt(lst[i]);

      grpOutput.States[iPos - 1] := cbsChecked;
    end;
  finally
    lst.Free;
  end;
end;

end.
